package mainService.services;

import java.util.ArrayList;

import mainService.HTTPService;
import mainService.ServiceParams;
import mainService.data.IpClient;
import mainService.data.UsersConnected;

/**
 * Servizio di Inserimento di un task del progetto.
 * 
 * @author Daniel Procop, Mattia Grecchi
 */
public class InserisciTaskService extends WriterServices implements HTTPService {

	@Override
	public ArrayList<String> getServiceResult(ServiceParams params) {

		String name = params.getValue("name");
		int ore;
		try {
			ore = Integer.parseInt(params.getValue("total_hours"));
		} catch (Exception e) {
			ore = 0;
		}

		int id_project = UsersConnected.getUsersConnected()
				.getUtente(IpClient.getSingleton().getIp()).getId_project();
		String descrizione = params.getValue("description");
		int worked_hours = 0;
		String expdate = params.getValue("expdate");

		String cmd = "INSERT INTO tasks (id_project, name, total_hours, worked_hours, description, expdate) VALUES "
				+ "('"
				+ id_project
				+ "', '"
				+ name
				+ "', '"
				+ ore
				+ "', '"
				+ worked_hours + "', '" + descrizione + "', '" + expdate +"')";
		boolean var = updateDb(cmd);
		if (!var) {// inserimento non corretto
			params.addParam("file", "inserimentoTaskFallito.html");
		} else {
			params.addParam("file", "inserimentoTaskRiuscito.html");
		}

		return (new HttpFileServer()).getServiceResult(params);
	}
}
